//
//  SHMenuFindViewController.m
//  吃货·上海
//
//  Created by lanou on 15/11/28.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "SHMenuFindViewController.h"
#import "AwesomeMenu.h"
#import "DessertCollectionViewController.h"
#import "ImageController.h"
#import <BmobSDK/Bmob.h>
#import "MBProgressHUD.h"
#import "SHMenuRootViewController.h"
#import "RegistViewController.h"
#import "MyCollectionController.h"

//#import ""

@interface SHMenuFindViewController () <AwesomeMenuDelegate ,MBProgressHUDDelegate>

@property(nonatomic ,strong)UIImageView *myImageView;

@property (nonatomic,strong)AwesomeMenu *menu;

@property (nonatomic,strong)UIView *labelView;

@property(nonatomic,strong)NSMutableArray *imageArray;

@property(nonatomic,strong)NSMutableArray *DishnameArray;

@property(nonatomic,assign)BOOL isSelect;

@property(nonatomic,assign)BOOL isFinish;

@end

@implementation SHMenuFindViewController
{
    UILabel *label1;
    UILabel *label2;
    UILabel *label3;
    UILabel *label4;
    UILabel *label5;
    
    MBProgressHUD *HUD;
}

- (NSMutableArray *)imageArray
{
    if (_imageArray == nil) {
        self.imageArray = [NSMutableArray array];
    }
    
    return _imageArray;
}

- (NSMutableArray *)DishnameArray
{
    if (_DishnameArray == nil) {
        self.DishnameArray = [NSMutableArray array];
    }
    
    return _DishnameArray;
}


- (void)viewWillAppear:(BOOL)animated{
    
    [_myImageView removeFromSuperview];
    [_menu removeFromSuperview];
    self.myImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 9; i <20; i++) {
        [array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]]];
    }
    
    [self setAnimationWithView:self.myImageView images:array animationTime:2];
    
    [self.view addSubview:_myImageView];
    
    [self updataMenu];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)setAnimationWithView:(UIImageView *)imageView images:(NSArray *)imageArray animationTime:(CGFloat)time
{
    [imageView removeFromSuperview];
    imageView.userInteractionEnabled = YES;
    imageView.animationImages = imageArray ;
    imageView.animationDuration = time * imageArray.count;
    imageView.animationRepeatCount = FLT_MAX;
    [imageView startAnimating];
}

- (void)updataMenu
{
    UIImage *storyMenuItemImage = [UIImage imageNamed:@"bg-menuitem.png"];
    UIImage *storyMenuItemImagePressed = [UIImage imageNamed:@"bg-menuitem-highlighted.png"];
    
    UIImage *starImage = [UIImage imageNamed:@"icon-star.png"];
    

    //
    AwesomeMenuItem *starMenuItem1 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage highlightedImage:storyMenuItemImagePressed ContentImage:starImage highlightedContentImage:nil];
    AwesomeMenuItem *starMenuItem2 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
        highlightedImage:storyMenuItemImagePressed
        ContentImage:starImage
        highlightedContentImage:nil];
    AwesomeMenuItem *starMenuItem3 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
                                                           highlightedImage:storyMenuItemImagePressed
                                                               ContentImage:starImage
                                                    highlightedContentImage:nil];
    AwesomeMenuItem *starMenuItem4 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
                                                           highlightedImage:storyMenuItemImagePressed
                                                               ContentImage:starImage
                                                    highlightedContentImage:nil];
    AwesomeMenuItem *starMenuItem5 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
                                                           highlightedImage:storyMenuItemImagePressed
                                                               ContentImage:starImage
                                                    highlightedContentImage:nil];
    
    NSArray *menus = [NSArray arrayWithObjects:starMenuItem1, starMenuItem2, starMenuItem3, starMenuItem4, starMenuItem5, nil];
    
    AwesomeMenuItem *startItem = [[AwesomeMenuItem alloc] initWithImage:[UIImage imageNamed:@"bg-addbutton.png"]
                                                       highlightedImage:[UIImage imageNamed:@"bg-addbutton-highlighted.png"]
                                                           ContentImage:[UIImage imageNamed:@"icon-plus.png"]
                                                highlightedContentImage:[UIImage imageNamed:@"icon-plus-highlighted.png"]];
    
    
    self.menu = [[AwesomeMenu alloc]initWithFrame:self.view.bounds startItem:startItem menuItems:menus];
    self.menu.delegate = self;
    
    self.menu.menuWholeAngle = M_PI_2;
    self.menu.farRadius = 110.0f;
    self.menu.endRadius = 100.0f;
    self.menu.nearRadius = 90.0f;
    self.menu.animationDuration = 0.3f;
    
    CGFloat pointY = self.view.center.y + self.view.center.y * 0.35;
    CGFloat pointX = self.view.center.x - self.view.center.x *0.7;
    
    self.menu.startPoint = CGPointMake(pointX , pointY);
    
    
    [self.view insertSubview:_menu aboveSubview:_myImageView];
    
}



- (void)awesomeMenu:(AwesomeMenu *)menu didSelectIndex:(NSInteger)idx
{
    switch (idx) {
        case 0:
            {
                NSLog(@"我的菜谱");
                SHMenuRootViewController *rootVc =[[SHMenuRootViewController alloc]init];
                [self.navigationController pushViewController:rootVc animated:NO];
            }
            break;
        case 1:
            {
                NSLog(@"我的收藏");
                MyCollectionController *mycollectionVc = [[MyCollectionController alloc]init];
                
                [self.navigationController pushViewController:mycollectionVc animated:NO];
                
            }
            break;
        case 2:
            {
                NSLog(@"我的点心");
                DessertCollectionViewController *searchVc = [[DessertCollectionViewController alloc]init];
                
                [self.navigationController pushViewController:searchVc animated:NO];
            }
            break;
        case 3:
            {
                NSLog(@"快速注册");
                RegistViewController *shareVc = [[RegistViewController alloc]init];
                [self.navigationController pushViewController:shareVc animated:NO];
            }
            break;
        case 4:
            {
                NSLog(@"快速预览");
                if (_isSelect == YES) {
                    UIAlertView *alert =[[UIAlertView alloc]initWithTitle:nil message:@"请耐心等待" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
                    [alert show];
                }else
                {
                    BmobQuery *bqeury = [BmobQuery queryWithClassName:@"Menu"];
                    [bqeury findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
                        if (array != nil) {
                            [self performSelectorInBackground:@selector(getImagesFromServer:) withObject:array];
                        }
                    }];
                }
                
                self.isSelect = YES;
                HUD =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
                
                HUD.labelText = @"loading";

                
            }
            break;
        default:
            break;
    }
    
    [self removeLabelFromSuperView];
    
}


- (void)getImagesFromServer:(NSArray *)array
{
    int i = 0;
    [self.imageArray removeAllObjects];
    NSMutableArray *arrayTit = [NSMutableArray array];
    
    for (BmobObject *obj in array) {
        
        BmobFile *file = [obj objectForKey:@"icon"];
        NSString *title = [obj objectForKey:@"Dishname"];
        NSData *data  =[NSData dataWithContentsOfURL:[NSURL URLWithString:file.url]];
        
        UIImage *image = [UIImage imageWithData:data];
        [arrayTit addObject:title];
        [self.imageArray addObject:image];
        i++;
        HUD.detailsLabelText = [NSString stringWithFormat:@"%d张",i];
    }
    
    if (_isSelect == YES ) {
        ImageController *imageVc =[[ImageController alloc]initWithImages:self.imageArray];
        imageVc.titleArray = arrayTit;
        [self.navigationController pushViewController:imageVc animated:NO];
    }
}

#pragma mark - stopAnimation
- (void)viewDidDisappear:(BOOL)animated
{
    _isSelect = NO;
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}


- (void)awesomeMenuDidFinishAnimationClose:(AwesomeMenu *)menu {

    [self removeLabelFromSuperView];
}

- (void)removeLabelFromSuperView
{
    [label1 removeFromSuperview];
    [label2 removeFromSuperview];
    [label3 removeFromSuperview];
    [label4 removeFromSuperview];
    [label5 removeFromSuperview];
}


- (void)awesomeMenuDidFinishAnimationOpen:(AwesomeMenu *)menu {
    
    [self.myImageView stopAnimating];
    
    self.myImageView.image = [UIImage imageNamed:@"root.jpg"];
    
    CGFloat X = self.menu.startPoint.x ;
    CGFloat Y = self.menu.startPoint.y - 150;
    
    label1 = [[UILabel alloc]initWithFrame:CGRectMake(X , Y + 5, 80, 20)];
    label1.text = @"我的菜谱";
    
    label2 = [[UILabel alloc]initWithFrame:CGRectMake(X + 65, Y + 35, 80, 20)];
    label2.text = @"我的收藏";
    
    label3 = [[UILabel alloc]initWithFrame:CGRectMake(X + 100, Y + 65, 80, 20)];
    label3.text = @"我的点心";
    
    label4 = [[UILabel alloc]initWithFrame:CGRectMake(X + 120, Y + 100, 80, 20)];
    label4.text = @"快速注册";
    
    label5 = [[UILabel alloc]initWithFrame:CGRectMake(X + 130, Y + 140, 80, 20)];
    label5.text = @"快速预览";
    
    NSArray *array = @[label1,label2,label3,label4,label5];
    for (UILabel *label in array) {
        label.textColor = [UIColor blueColor];
        [self.view insertSubview:label aboveSubview:_menu];
        [self setLabelAnimation:label];
    }
}

#pragma mark  - setLabel's animation
- (void)setLabelAnimation:(UILabel *)label
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    NSValue *pointStart = [NSValue valueWithCGPoint:CGPointMake(self.view.bounds.size.width, label.frame.origin.y)];
    NSValue *pointEnd = [NSValue valueWithCGPoint:CGPointMake(label.center.x, label.center.y)];
    animation.values = @[pointStart , pointEnd];
    animation.duration = 0.2;
    [label.layer addAnimation:animation forKey:nil];
    
    [UIView animateWithDuration:5 animations:^{
        label.alpha = 0;
    }];
}


@end
